package com.itHuangTao.tank.components;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.texture.Texture;
import com.almasb.fxgl.time.TimerAction;
import com.itHuangTao.tank.Config;
import com.itHuangTao.tank.GameType;
import com.itHuangTao.tank.effects.EnemyAppearanceEffect;

public class EnemyComponent extends Component {

    private TankComponent tankComponent;
    private EffectComponent effectComponent;

    @Override
    public void onAdded() {
        //给这个组件在一开始就加入产生效果
        entity.getComponent(EffectComponent.class).startEffect(new EnemyAppearanceEffect());
    }

    @Override
    public void onUpdate(double tpf) {
        if(entity.getComponent(EffectComponent.class).hasEffect(EnemyAppearanceEffect.class)){
            return;
        }

        //冻结敌人（当玩家拾取冻结道具）
        if (FXGL.getb("freezeEnemy")) {
            return;
        }
        Dir moveDir = tankComponent.getMoveDir();
        //每一帧只有百分之2.5的几率进行改变方向
        if (FXGLMath.randomBoolean(0.025)) {
            //枚举值数组values[]
            moveDir = FXGLMath.random(Dir.values()).get();
        }
        //控制移动方向
        switch (moveDir) {
            case UP -> tankComponent.moveUp();
            case DOWN -> tankComponent.moveDown();
            case LEFT -> tankComponent.moveLeft();
            case RIGHT -> tankComponent.moveRight();
            default -> {
            }
        }

        //敌人坦克每一帧有百分之3的机会发射子弹
        if (FXGLMath.randomBoolean(0.03)) {
            tankComponent.shoot();
        }

    }
}
